package org.dromara.learning_xq.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.dromara.learning_xq.entity.dto.AsKnowledgeMapDto;
import org.dromara.learning_xq.service.AsKnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("asKnowledgeMap")
@Tag(name = "知识地图模板")
public class AsKnowledgeController {

    @Autowired
    private AsKnowledgeService asKnowledgeService;


    /**
     * 增加知识地图模板
     *
     * @param asKnowledgeMapDto 知识地图DTO对象，包含要增加的模板信息
     * @return 更新后的知识地图DTO列表
     */
    @PostMapping("increaseAsKnowledgeMap")
    @Operation(summary = "增加知识地图模板")
    public List<AsKnowledgeMapDto> increaseAsKnowledgeMap(@RequestBody AsKnowledgeMapDto asKnowledgeMapDto) {
        try {
            // 调用服务层方法增加记录
            List<AsKnowledgeMapDto> updatedList = asKnowledgeService.increaseAsKnowledgeMap(asKnowledgeMapDto);

            if (updatedList != null && !updatedList.isEmpty()) {
                // 添加成功，返回更新后的列表
                return updatedList;
            } else {
                // 如果增加失败，返回空列表或抛出异常
                throw new RuntimeException("添加失败，未返回有效的记录。");
            }
        } catch (Exception e) {
            // 捕获并处理异常
            System.err.println("添加知识图谱失败: " + e.getMessage());
            // 根据需要可以重新抛出异常或返回一个空列表
            return Collections.emptyList(); // 或者 return null;
        }
    }

    @PostMapping("selverAll")
    @Operation(summary = "查询所有知识地图模板")
    public ResponseEntity<List<AsKnowledgeMapDto>> selverAll(@RequestBody AsKnowledgeMapDto asKnowledgeMapDto) {
        try {
            List<AsKnowledgeMapDto> knowledgeMaps = asKnowledgeService.selverAll(asKnowledgeMapDto);
            return ResponseEntity.ok(knowledgeMaps);
        } catch (Exception e) {
            // Log the exception if needed
            return ResponseEntity.status(500).body(null);
        }
    }

    /**
     * 更新知识地图模板
     *
     * @param asKnowledgeMapDto 知识地图DTO对象，包含要更新的模板信息
     * @return 更新后的知识地图DTO列表
     */
    @PostMapping("updateAsKnowledgeMap")
    @Operation(summary = "更新知识地图模板")
    public ResponseEntity<List<AsKnowledgeMapDto>> updateAsKnowledgeMap(@RequestBody AsKnowledgeMapDto asKnowledgeMapDto) {
        try {
            // 调用服务层方法更新记录
            List<AsKnowledgeMapDto> updatedList = asKnowledgeService.updateAsKnowledgeMap(asKnowledgeMapDto);

            if (updatedList != null && !updatedList.isEmpty()) {
                // 更新成功，返回更新后的列表
                return ResponseEntity.ok(updatedList);
            } else {
                // 如果更新失败，返回一个错误响应
                return ResponseEntity.badRequest().body(Collections.emptyList());
            }
        } catch (Exception e) {
            // 捕获并处理异常
            System.err.println("更新知识图谱失败: " + e.getMessage());
            // 返回一个错误响应
            return ResponseEntity.status(500).body(Collections.emptyList());
        }
    }

    @PostMapping("removeBatchByIds")
    @Operation(summary = "删除知识地图模板")
    public ResponseEntity<Boolean> removeBatchByIds(@RequestBody List<Long> ids) {
        // 调用服务层方法删除记录
        boolean result = asKnowledgeService.removeBatchByIds(ids);

        if (result) {
            // 删除成功，返回true
            return ResponseEntity.ok(true);
        } else {
            // 如果删除失败，返回false
            return ResponseEntity.badRequest().body(false);
        }
    }
}
